﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;


using System.Linq;
using System.Text;
using WinnerCMS.Common;
using EntityFramework.Extensions;
using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_CounterDiaries。
    /// </summary>
    public class CounterDiaryDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(CounterDiary model)
        {
            using (Entities db = new Entities())
            {
                db.CounterDiares.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="time"></param>
        public void Delete(DateTime time)
        {
            using (Entities db = new Entities())
            {
                db.CounterDiares.Where(x => x.Time >= time).Delete();
            }
        }
        /// <summary>
        /// 清空日志
        /// </summary>
        public void Delete()
        {
            using (Entities db = new Entities())
            {
                //Msql、mysql、oracle支持
                db.Database.ExecuteSqlCommand("Truncate table CMS_CounterDiaries");
            }
        }


        /// <summary>
        /// 按月统计访问量
        /// </summary>
        /// <returns></returns>
        public IList<CounterDiary> Month()
        {
            using (Entities db = new Entities())
            {
                var query = db.CounterDiares
                    .Where(x => x.Year == DateTime.Now.Year).GroupBy(x => x.Month)
                    .Select(x => new { Month = x.Key, Count = x.Count() }).ToArray();
                var list = new List<dynamic>();
                for (var i = 1; i <= 12; i++)
                {
                    list.Add(new { Month = i, Count = 0 });
                }

                return query.Concat(list).GroupBy(x => x.Month).Select(x => new CounterDiary { Month = x.Key, SUM = x.Sum(y => y.Count) }).ToArray();
            }

        }

        /// <summary>
        /// 按日统计访问量
        /// </summary>
        /// <returns></returns>
        public IList<CounterDiary> Day()
        {
            using (Entities db = new Entities())
            {
                var query = db.CounterDiares.Where(x => x.Year == DateTime.Now.Year && x.Month == DateTime.Now.Month).GroupBy(x => x.Day).Select(x => new { Day = x.Key, Count = x.Count() }).ToArray();
                var list = new List<dynamic>();
                for (var i = 1; i <= DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month); i++)
                {
                    list.Add(new { Day = i, Count = 0 });
                }

                return query.Concat(list).GroupBy(x => x.Day).Select(x => new CounterDiary { Day = x.Key, SUM = x.Sum(y => y.Count) }).ToArray();
            }
        }

        /// <summary>
        /// 按小时统计访问量
        /// </summary>
        /// <returns></returns>
        public IList<CounterDiary> Hour()
        {
            using (Entities db = new Entities())
            {
                var query = db.CounterDiares.Where(x => x.Year == DateTime.Now.Year && x.Month == DateTime.Now.Month).GroupBy(x => x.Hour).Select(x => new { Hour = x.Key, Count = x.Count() }).ToArray();
                var list = new List<dynamic>();
                for (var i = 1; i <= 24; i++)
                {
                    list.Add(new { Hour = i, Count = 0 });
                }

                return query.Concat(list).GroupBy(x => x.Hour).Select(x => new CounterDiary() { Hour = x.Key, SUM = x.Sum(y => y.Count) }).ToArray();
            }
        }

        /// <summary>
        /// 按地区统计访问量(前20位)
        /// </summary>
        /// <returns></returns>
        public IList<CounterDiary> Where()
        {
            using (Entities db = new Entities())
            {
                var query = db.CounterDiares.GroupBy(x => x.Where).Select(x => new { Where = x.Key, MaxTime = x.Max(y => y.Time), Count = x.Count() });

                return query.ToArray().Select(x => new CounterDiary() { Where = x.Where, MaxTime = x.MaxTime, Count = x.Count }).ToArray();
            }
        }
        /// <summary>
        /// 统计在线人数
        /// </summary>
        /// <returns></returns>
        public int TotalOnline(int iOnlineActiveTime)
        {
            DateTime newtime = DateTime.Now.AddMinutes(-iOnlineActiveTime);

            using (Entities db = new Entities())
            {
                return db.CounterDiares.Count(x => x.Time >= newtime);
            }

        }
        /// <summary>
        /// 被访问页面统计(前20位)
        /// </summary>
        /// <returns></returns>
        public IList<CounterDiary> Page()
        {
            using (Entities db = new Entities())
            {
                var query = db.CounterDiares.GroupBy(x => x.Page).Select(x => new { Page = x.Key, MaxTime = x.Max(y => y.Time), Count = x.Count() });

                return query.ToArray().Select(x => new CounterDiary() { Page = x.Page, MaxTime = x.MaxTime, Count = x.Count }).ToArray();
            }
        }
        /// <summary>
        /// 总访问量
        /// </summary>
        /// <returns></returns>
        public int AllCount()
        {
            using (Entities db = new Entities())
            {
                return db.CounterDiares.Count();
            }
        }
        /// <summary>
        /// 来路统计(前20位)
        /// </summary>
        /// <returns></returns>
        public IList<CounterDiary> Come()
        {
            using (Entities db = new Entities())
            {
                var query = db.CounterDiares.GroupBy(x => x.Come).Select(x => new { Come = x.Key, MaxTime = x.Max(y => y.Time), Count = x.Count() });

                return query.ToArray().Select(x => new CounterDiary
                {
                    Come = x.Come, 
                    MaxTime = x.MaxTime, 
                    Count = x.Count
                }).ToArray();
            }
        }
        /// <summary>
        /// 今年访问量
        /// </summary>
        /// <returns></returns>
        public object ThisYear()
        {
            int y = DateTime.Now.Year;

            using (Entities db = new Entities())
            {
                return db.CounterDiares.Count(x => x.Year == y);
            }
        }

        /// <summary>
        /// 本月访问量
        /// </summary>
        /// <returns></returns>
        public int ThisMonth()
        {
            int y = DateTime.Now.Year;
            int m = DateTime.Now.Month;

            using (Entities db = new Entities())
            {
                return db.CounterDiares.Count(x => x.Year == y && x.Month == m);
            }
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<CounterDiary> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.CounterDiares
                        select x;
                pp.RecordCount = q.Count();

                return q.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToArray();
            }
        }
    }
}